package com.fang.service;

import com.fang.mapper.PlaceMapper;
import com.fang.mapper.ProviceMapper;
import com.fang.pojo.Place;
import com.fang.pojo.Provice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@SuppressWarnings("all")
@Service
@Transactional
public class PlaceServiceImpl implements PlaceService {

    @Autowired
    private PlaceMapper placeMapper;

    @Autowired
    private ProviceMapper proviceMapper;

    @Override
    public List<Place> findByProviceIdPage(Integer page, Integer rows, Integer proviceId) {
        Integer start = (page - 1) * rows;
        return placeMapper.findByProviceIdPage(start, rows, proviceId);
    }

    @Override
    public Integer findByProviceIdCounts(Integer proviceId) {
        return placeMapper.findByProviceIdCounts(proviceId);
    }

    @Override
    public void save(Place place) {
        // 查询原始省份信息
        Provice provice = proviceMapper.findOne(place.getProviceId());
        // 更新该省份的景点个数
        provice.setPlaceCounts(provice.getPlaceCounts() + 1);
        proviceMapper.update(provice);
        // 保存景点信息
        placeMapper.save(place);
    }

    @Override
    public void delete(Integer id) {
        // 查询景点
        Place place = placeMapper.findOne(id);
        // 根据景点所属省份id查询省份
        Provice provice = proviceMapper.findOne(place.getProviceId());
        // 更新省份景点个数
        provice.setPlaceCounts(provice.getPlaceCounts() - 1);
        proviceMapper.update(provice);
        // 删除景点
        placeMapper.delete(id);
    }

    @Override
    public Place findOne(Integer id) {
        return placeMapper.findOne(id);
    }

    @Override
    public void update(Place place) {
        placeMapper.update(place);
    }
}
